package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class RegisteredActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    EditText et_uName, et_uPassword, et_uPassword2;
    CheckBox cb_football, cb_basketball, cb_badminton, cb_tableTennis;
    RadioGroup rg_sx;
    RadioButton rb_boy, rb_girl;
    Button bt_registered;
    String hobby = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);
        init_view();
        bt_registered.setOnClickListener(this);
        cb_football.setOnCheckedChangeListener(this);
        cb_basketball.setOnCheckedChangeListener(this);
        cb_badminton.setOnCheckedChangeListener(this);
        cb_tableTennis.setOnCheckedChangeListener(this);
    }

    public void init_view() {
        et_uName = findViewById(R.id.et_uName);
        et_uPassword = findViewById(R.id.et_uPassword);
        et_uPassword2 = findViewById(R.id.et_uPassword2);
        cb_football = findViewById(R.id.cb_football);
        cb_basketball = findViewById(R.id.cb_basketball);
        cb_badminton = findViewById(R.id.cb_badminton);
        cb_tableTennis = findViewById(R.id.cb_tableTennis);
        rg_sx = findViewById(R.id.rg_sx);
        rb_boy = findViewById(R.id.rb_boy);
        rb_girl = findViewById(R.id.rb_girl);
        bt_registered = findViewById(R.id.bt_registered);
    }

    @Override
    public void onClick(View v) {
        String uName = et_uName.getText().toString().trim();
        String uPassword = et_uPassword.getText().toString().trim();
        String uPassword2 = et_uPassword2.getText().toString().trim();
        String sex = "";
        if (uName.isEmpty() || uPassword.isEmpty() || uPassword2.isEmpty()) {
            Toast.makeText(this, "用户名或密码未填写完整", Toast.LENGTH_SHORT).show();
        } else {
            if (uName.length() < 5 || uPassword.length() < 6 || uPassword2.length() < 6) {
                Toast.makeText(this, "用户名或密码长度不符合规定，请重新输入", Toast.LENGTH_SHORT).show();
            } else {
                if (uPassword.equals(uPassword2)) {
                    if (cb_football.isChecked() || cb_basketball.isChecked() || cb_badminton.isChecked() || cb_tableTennis.isChecked()) {
                        if (rb_boy.isChecked() || rb_girl.isChecked()) {
                            User user = new User();
                            user.setUsername(uName);
                            user.setPassword(uPassword);
                            if (rb_boy.isChecked()) {
                                sex = "男";
                            } else {
                                sex = "女";
                            }
                            user.setSex(sex);
                            user.setHobby(hobby);
                            Intent intent = new Intent(RegisteredActivity.this, LoginActivity.class);
                            intent.putExtra("user", user);
                            setResult(50, intent);
                            finish();
                        } else {
                            Toast.makeText(this, "请选择你的性别", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(this, "请选择至少一个爱好", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "两次密码输入不一致，请重新输入", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_football:
                hobby += "足球  ";
                break;
            case R.id.cb_basketball:
                hobby += "篮球  ";
                break;
            case R.id.cb_badminton:
                hobby += "羽毛球  ";
                break;
            case R.id.cb_tableTennis:
                hobby += "乒乓球";
                break;
        }
    }
}